package servlet;

import bean.Result;
import com.google.gson.Gson;
import dao.BackLoginDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "BackLoginServlet", value = "/BackLoginServlet.s")
public class BackLoginServlet extends HttpServlet {
    private BackLoginDao dao = new BackLoginDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Result result;
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Map<String, Object> res = dao.login(username, password);
        if (res != null){
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            result = new Result(1, "登陆成功!", res);
        }else {
            result = new Result(0, "登陆失败", null);
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);


    }
}
